﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors; 
using System.Data.SqlClient;
using System.Collections;
using UFIDA.U8C.AnLianDB;

namespace AnLianSysClient
{
    public partial class frmProject : DevExpress.XtraEditors.XtraForm
    {
         
        private frmConstruction refCustomerfrm = new frmConstruction();
        const string regKey = "DevExpress\\XtraGrid\\Layouts\\project";

        public frmProject()
        {
            InitializeComponent(); 
        }

        /// <summary>界面加载 初始化数据
        /// 
        /// </summary>
        private void initData()
        {
            try
            {
                 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
          

        /// <summary>合并NC图书主档与本地图主档信息 获取不同的行
        /// 
        /// </summary>
        /// <param name="dt1"></param>
        /// <param name="dt2"></param>
        /// <returns></returns>
        public DataTable CompareTwoDataTable(DataTable dt1, DataTable dt2)
        {
            try
            {
                if (dt1 == null || dt2 == null || dt1.Rows.Count <= 0)
                {
                    return null;
                }
               //获取两个数据源的差集
                IEnumerable<DataRow> query2 = dt1.AsEnumerable().Except(dt2.AsEnumerable(), DataRowComparer.Default);
                //两个数据源的差集集合
                if (query2.Count() <= 0)
                {
                    return null;
                }
                DataTable dt3 = query2.CopyToDataTable();
                return dt3;
            }            
            catch(Exception ex){
                MessageBox.Show(ex.Message);
                return null;
            }            
        }
                       
        private void GdvProject_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator && e.RowHandle >= 0)
                e.Info.DisplayText = (e.RowHandle + 1).ToString();

        }

        private void FrmProject_Load(object sender, EventArgs e)
        {
            gdproject.MainView.RestoreLayoutFromRegistry(regKey);//读取（还原）布局 
        }

        private void FrmProject_FormClosed(object sender, FormClosedEventArgs e)
        {
            gdproject.MainView.SaveLayoutToRegistry(regKey); //保存布局
        }

        private void BtnQuery_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Query();
        }

        private void Query()
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select *, case iflag when 1 then '是' else '否' end cflag from  wazh_project ");

            if (!string.IsNullOrEmpty(edtCode.Text))
            {
                sql.Append(" and project like '%" + edtCode.Text + "%'");
            }

            if (!string.IsNullOrEmpty(edtName.Text))
            {
                sql.Append(" and projectname like '%" + edtName.Text + "%'");
            }

            if (!string.IsNullOrEmpty(edtclass.Text))
            {
                sql.Append(" and jobtype like '%" + edtclass.Text + "%'");
            }

            if (!string.IsNullOrEmpty(cbsync.Text))
            {
                if (cbsync.Text.Equals("是"))
                {
                    sql.Append(" and iflag = 1");
                }
                else
                {
                    sql.Append(" and iflag = 0");
                } 
            }

            gdproject.DataSource = null;
            dsproject = UFIDA.MYSQL.DB.DbHelperSQL.Query(sql.ToString(), "project", UFIDA.U8C.AnLianDB.common.mysqlconn());
            gdproject.DataSource = bdsProject;
            bdsProject.DataSource = dsproject;
            bdsProject.DataMember = "project";
        }
    }
}